package com.bitejiuyeke.forum.services.impl;

import com.bitejiuyeke.forum.common.AppResult;
import com.bitejiuyeke.forum.common.ResultCode;
import com.bitejiuyeke.forum.dao.MessageMapper;
import com.bitejiuyeke.forum.exception.ApplicationException;
import com.bitejiuyeke.forum.model.Message;
import com.bitejiuyeke.forum.model.User;
import com.bitejiuyeke.forum.services.IMessageService;
import com.bitejiuyeke.forum.services.IUserService;
import com.bitejiuyeke.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 666
 * Date: 2024-02-18
 * Time: 15:06
 */
@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {
       @Resource
    private MessageMapper messageMapper;
     @Resource
       private IUserService userService;
    @Override
    public void create(Message message) {
     //非空检验
        if(message == null || message.getPostUserId() == null || message.getReceiveUserId() == null ||
                StringUtil.isEmpty(message.getContent())) {
     //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验接受者是否存在
        User user = userService.selectById(message.getReceiveUserId());
        if (user == null || user.getDeleteState() == 1){
           // log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置默认值
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);
        //设置创建与更新时间
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        //调用dao层
        int i = messageMapper.insertSelective(message);
        if(i != 1) {
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public Integer selectUnreadCount(Long receiverUserId) {
        //非空校验
        if(receiverUserId == null || receiverUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用dao层
        Integer count = messageMapper.selectUnreadCount(receiverUserId);
        //正常不可能出现null
        //如果为null，抛出异常
        if(count == null) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {

                if(receiveUserId == null || receiveUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用dao层
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);

        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
     //0未读，1已读，2已回复
        if(id == null || id <= 0 || state< 0 || state > 2) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //构造更新对象
        Message updateMessage = new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        updateMessage.setUpdateTime(new Date());
        //调用dao层
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if(row != 1) {
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Message selectById(Long id) {

                if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //调用dao层
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public void reply(Long repliedId, Message message) {

        //非空检验
        if (repliedId == null || repliedId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message existsMessage = messageMapper.selectByPrimaryKey(repliedId);
      if(existsMessage == null || existsMessage.getDeleteState() ==1){
          log.warn(ResultCode.FAILED.toString());
          throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
      }
      //更新状态
      updateStateById(repliedId,(byte)2);
      //回复内容写入数据库
      create(message);
    }
    }
